
	/**
	 * HLP Validate
	**/
	
	panel.hlp.validate_mask = {
		
		text: /^[a-zA-Z0-9\s\#\/\^\$\!\?\@\%\&\*\.\:\;\,\(\)\+\=\_\-\©\®\\']+$/,
		number: /^[0-9]+$/,
		az: /^[a-zA-Z]+$/,
		azn: /^[a-zA-Z0-9]+$/
		
	};
	
	panel.hlp.validate = function( v, p, n ) {
			
	try {

		switch( $type(v) ) {
		
		case "element": 
			var _el = $(v).removeClass("validation_failed");
			v = $(v).get("value"); 
			break;
		
		case false:
			throw "VALIDATION ERROR (A1): Passed value for validation is unknown/undefined";
			break;
		
		}	// switch
		
		if( $defined(n) && $type(n) != "string" && $type(n) != "number" ) {
			n = v.toString(); }
			
		if( $type(p) == "string" ) {
			p = [p]; } 
		
		p = $A(p);				
	
		p.each( function( _t, _i ) {
		
			switch( $type(_t) ) {
		
			case "string":
			
				switch( _t.toLowerCase() ) {
				
				case "string!":
					if( $type(v) != "string" ) { throw n + " is not string"; }
					break;
					
				case "number!":
					if( $type(v) != "number" ) { throw n + " is not number"; }
					break;
					
				case "numeric!":
					if( isNaN(v.toInt()) ) { throw n + " is not numeric"; }
					break;
					
				case "~integer":
					v = parseInt(v).toIntZ();
					break;
					
				case "scalar!":
					if( $type(v) != "string" && $type(v) != "number" && $type(v) != "boolean" ) { throw n + " is not scalar"; }
					break;
				
				case "|trim": v = v.toString().trim(); break;
				case "|encode": v = encodeURIComponent( v.toString() ); break;
				case "|decode": v = decodeURIComponent( v.toString() ); break;
				case "|case-lower": v = v.toLowerCase(); break;
				
				case "not-empty!": if( !v.length ) { throw n + " is empty"; } break;
				
				/* Presets */
				case "[allow-empty]": if( !v.length ) { throw ""; } break;
				
				/* Non empty value */
				case "[ne]":
					v = panel.hlp.validate(v, "not-empty!", n);
					break;

				case "[ip]":
					
					v = panel.hlp.validate(
						v, 
						[
							"scalar!", "|trim", "not-empty!", 
							[/^(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])$/, "Incorrect IP address"],
							"|encode"
						],
						n
					);
					
					break;
				
					
				/* Non empty scalar + encodeURI + a-z 0-9 limited */
				case "[nlt]":
					
					v = panel.hlp.validate(
						v, 
						[
							"scalar!", "|trim", "not-empty!", 
							[/^[a-zA-Z0-9\ \(\)\+\-\.\/]+$/, "Allowed A-z, 0-9, +, -, slash."],
							"|encode"
						],
						n
					);
					
					break;
				
				/* Non empty scalar + encodeURI + (a-z 0-9 + punct) */
				case "[text]":
					
					v = panel.hlp.validate(
						v, 
						[
							"scalar!", "|trim", "not-empty!", 
							[/^[a-zA-Z0-9\s\#\/\^\$\!\?\@\%\&\*\.\:\;\,\(\)\+\=\_\-\©\®\\']+$/, "allowed A-z, 0-9, punctuation signs."],
							"|encode"
						],
						n
					);
					
					break;
					
				case "[percent-0-100]":
				
					v = panel.hlp.validate(
						v,
						[
							"scalar!", "|trim",
							[/^(0|100|[1-9][0-9]{0,1})$/, "Percent in range between 0 and 100."]
						],
						n
					);
				
					break;
					
				/* URL absolute */
				case "[url-a]":
					
					v = panel.hlp.validate(
						v, 
						[
							"scalar!", "|trim", "not-empty!", "|case-lower",
							[/^((http\:\/\/)|(https\:\/\/))([a-zA-Z0-9\_\-]+[\.])+[a-zA-Z0-9]+$/, "allowed like http://domain.com"], 
							"|encode"
						],
						n
					);
					
					break;
					
				/* URL absolute + relative */
				case "[url-ar]":
					
					v = panel.hlp.validate(
						v, 
						[
							"scalar!", "|trim", "not-empty!", "|case-lower",
							[/^(((http\:\/\/)|(https\:\/\/))([a-zA-Z0-9\_\-]+[\.])+[a-zA-Z0-9]+|([a-zA-Z0-9\_\-]+\.)?[a-zA-Z0-9]+)$/, "allowed like http://domain.com or some_url.php"], 
							"|encode"
						],
						n
					);
					
					break;
				
				/* URL relative */
				case "[url-r]":
					
					v = panel.hlp.validate(
						v, 
						[
							"scalar!", "|trim", "not-empty!", "|case-lower",
							[/^([a-zA-Z0-9\_\-]+\.)?[a-zA-Z0-9\-\_]+$/, "allowed like some_url.php"], 
							"|encode"
						],
						n
					);
					
					break;
					
				/* URL without http */
				case "[url-rp]":
					
					v = panel.hlp.validate(
						v, 
						[
							"scalar!", "|trim", "not-empty!", "|case-lower",
							[/^[a-zA-Z0-9\-]+([\.][a-zA-Z0-9]+){1,2}?$/, "allowed like hostname.com"], 
							"|encode"
						],
						n
					);
					
					break;
					
				case "[integer-positive]":
					
					v = panel.hlp.validate(
						v,
						[ "scalar!", "|trim", [/^[\d]+$/, "number"] ],
						n
					);
					
					break;
					
				case "[hex-color]":
					
					v = panel.hlp.validate(
						v,
						[
							"scalar!", "|trim",
							[/^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$/, "HEX Color"]
						],
						n
					);
					
					break;
					
				case "[position]":
				
					v = panel.hlp.validate(
						v,
						[
							"scalar!", "|trim",
							[/^[1-9][0-9]{0,3}$/, "Number in range between 1 and 9999."]
						],
						n
					);
				
					break;
						
				case "[price-f]":
				
				
					v = panel.hlp.validate(
						v,
						[
							"numeric!", "|trim",
							[/^([0]|[1-9]{1}|[1-9][0-9]{1,3})(\.[0-9]{1,2})?$/, "Number in range between 0.00 and 9999.99."]
						],
						n
					);
				
					break;
					
				case "[price]":
				
				
					v = panel.hlp.validate(
						v,
						[
							"numeric!", "|trim",
							[/^([0]|[1-9]{1}|[1-9][0-9]{1,3})$/, "Number in range between 0 and 9999."]
						],
						n
					);
				
					break;
					
				case "[price+]":
				
					v = panel.hlp.validate(
						v,
						[
							"numeric!", "|trim",
							[/^([1-9][0-9]{1,3})$/, "Number in range between 1 and 9999."]
						],
						n
					);
				
					break;
					
				case "[percent-1-99]":
				
					v = panel.hlp.validate(
						v,
						[
							"scalar!", "|trim",
							[/^[1-9][0-9]{0,1}$/, "Percent in range between 1 and 99."]
						],
						n
					);
				
					break;
					
				case "[+-percent-1-99]":
				
					v = panel.hlp.validate(
						v,
						[
							"scalar!", "|trim",
							[/^[\+\-]?[1-9][0-9]{0,1}$/, "Percent in range between (+,-) 1 and 99."]
						],
						n
					);
				
					break;
					
				case "[+-percent-price]":
				
					v = panel.hlp.validate(
						v,
						[
							"scalar!", "|trim",
							[/^([\-])?([0-9]{1,4})$/, "Percent in range between -1..-99 and +1..999."]
						],
						n
					);
				
					break;
					
				case "[+-price+]":
				
					v = panel.hlp.validate(
						v,
						[
							"scalar!", "|trim",
							[/^[\+\-]?[1-9][0-9]{0,3}$/, "Number in range between (+,-) 1 and 9999."]
						],
						n
					);
				
					break;
					
				case "[+-price]":
				
					v = panel.hlp.validate(
						v,
						[
							"scalar!", "|trim",
							[/^[\+\-]?([0]|[1-9]{1}|[1-9][0-9]{1,3})$/, "Number in range between (+,-) 0 and 9999."]
						],
						n
					);
				
					break;
					
				case "[price-modifier]":
				
					v = panel.hlp.validate(
						v,
						[
							"[allow-empty]", "scalar!", "|trim",
							[/^[\-\+]?[0-9]{1,4}$/, "Number in range between 0 and 9999 and +, - signs."]
						],
						n
					);
				
					break;
					
				case "[login]":
					
					v = panel.hlp.validate(
						v,
						[
							"scalar!",
							[/^[0-9a-zA-Z]+$/, "A-z, 0-9"]
						],
						n
					);
					
					break;
				
				case "[password]":
					
					v = panel.hlp.validate(
						v,
						[
							"scalar!",
							[/^[0-9a-zA-Z]+$/, "A-z, 0-9"]
						],
						n
					);
					
					break;
					
				
				default:
					throw "VALIDATION ERROR (A2): Unknown validation method: " + _t.toString();
					break;
				
				}	// switch
				
				break;
				
			case "regexp":
				
				if( v.toString().match(_t) == null ) {
					throw n + " does not match with regular expression:\n" + _t.toString(); }
				
				break;
				
			case "array":
			
				switch( $type(_t[0]) ) {
				
				case "regexp":
				
					if( v.toString().match(_t[0]) == null ) {
						
						throw [
						 
							n, " (", v.toString(), ") ", 
							"does not match with regular expression:\n", 
							_t[1].toString()
							
						].join(""); 
						
					}	// if
				
					break;
				
				case "string":
				
					switch( _t[0].toLowerCase() ) {
						
					case "[passed]":
						break;
						
					case "[failed]":
						throw _t[1];
						break;
					
					case "max-length":

						if( v.length > _t[1] ) {
							throw n + " length > " + _t[1]; }
					
						break;
						
					case "..":
					
						if( v < _t[1] || v > _t[2] ) {
							throw n + " not in range " + _t[1] + ".." + _t[2]; }
					
						break;
						
					default:
						throw "VALIDATION ERROR (A5): Unknown validation method: " + _t[0].toString();
						break;
						
					} // switch
					
					break;
					
				default:
					throw "VALIDATION ERROR (A4): Unknown validation method type: " + $type(_t[0]).toString();
					break;
				
				}	// switch
			
				break;
			
			default:
				throw "VALIDATION ERROR (A3): Unknown validation method type: " + $type(_t).toString();
				break;
			
			}	// switch
			
		} );	// each

		return v;
		
	} catch( e ) {
	
		// Handler for allow empty
		if( $type(e) == "string" && e == "" ) {
			return v; }
		
		if( $defined(_el) ) {
			
			_el.addClass("validation_failed");
			
			if( _el.focus instanceof Function ) {
				_el.focus(); }
				
			var sp = panel.hlp.getParentByProperty(_el, "_on_error_scroll");
			
			if( $defined(sp) ) {
				
				Ext.get(_el).scrollToTop(sp, false, function(el) {
								
					el.frame("FF0000", 1, { duration: 1 });
					
				});
				
			}
		
		}
			
		throw e;
	
	}	// try

	}	// validate
	